package com.rk.msm.receiver;

import com.rabbitmq.client.Channel;
import com.rk.common.constant.MqConst;
import com.rk.common.exception.YyghException;
import com.rk.common.result.ResultCodeEnum;
import com.rk.hosp.vo.msm.MsmVo;
import com.rk.msm.service.MsmService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSendException;
import org.springframework.stereotype.Component;



@Component
@Slf4j
@RabbitListener(queues = MqConst.QUEUE_EMAIL)
public class EmailReceiver {


    @Autowired
    private MsmService msmService;


    @RabbitHandler
    public void process(MsmVo msmVo, Message message, Channel channel) throws Exception {

        System.out.println("邮件消费者收到的消息为:"+msmVo);
        try {
            msmService.send(msmVo);
            System.err.println("发送成功,开启手动确认!");
            //channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }catch (MailSendException e){
            System.err.println("出错 发送失败");
            throw new YyghException(ResultCodeEnum.Email_ERROR);
        }catch (Exception e) {
            e.printStackTrace();
            System.err.println("出错 发送失败");
        }
    }

}
